#include "service.h"

Service::Service(QObject *parent)
    : QObject(parent)
{
    QObject::connect(&m_nam, SIGNAL(finished(QNetworkReply*)),
                     this, SLOT(responseHandler(QNetworkReply*)));
}


void Service::callServiceGET(QString urlString)
{
    QUrl url(urlString);
    QNetworkRequest req(url);
    qDebug("calling:");
    qDebug(urlString.toUtf8());

    m_nam.get(req);
}

void Service::callServiceDELETE(QString urlString)
{
    QUrl url(urlString);
    QNetworkRequest req(url);
    m_nam.deleteResource(req);
}

void Service::callServicePOST(QString urlString, QString data)
{
    QUrl url(urlString);
    QNetworkRequest req(url);

    m_nam.post(req,data.toUtf8());
}

void Service::responseHandler(QNetworkReply *finished)
{
    if ( finished->error() != QNetworkReply::NoError )
    {
        QNetworkReply::NetworkError err = finished->error();
        onError(err);
//        finished->close();
        finished->deleteLater();
    } else {
        QByteArray data = finished->readAll();
        onResponse(data);
//        finished->close();
        finished->deleteLater();
    }
}
